var mongoose = require('mongoose');
const DataModel = require('../models/cate.model');
const newsDataModel = require('../models/news.model')
exports.create = function(req,res,next){
	const dataModel = new DataModel(req.body);

	dataModel.save()
	.then(data=>{
		res.json(data);
	})
}

exports.get = function(req,res,next){
	var id = req.params.id;

	DataModel.findById(id, function (err, data) {
	  res.json(data);
	})
}


exports.update = function(req,res,next){
	const id = req.params.id;

	DataModel.findByIdAndUpdate(id, {$set: req.body}, {new:false})
	.then(data=>{
		res.json(data);
	})

}

exports.remove = function(req,res,next){
	const id = req.params.id;
	var ids=[];
	DataModel.findOne({_id:id},function(err,doc){
		if(doc){
			ids = [doc._id];
			doc.getChildren().then(function(docs){
				for(var i=0;i<docs.length;i++){
					ids.push(docs[i]._id);
				}
				DataModel.remove({_id:{$in:ids}})
				.then(data=>{
					res.json({'msg':"delete success",'status':200});
				})
			});
		}
	})

}

function reverseTree(data,pid){
	var result=[];
	var temp;
	var data=JSON.parse(JSON.stringify(data));
	for(var i in data){
		if(data[i].parentId==pid){
			console.log(data[i].parentId);
			result.push(data[i]);

			temp = reverseTree(data,data[i]._id);
			if(temp.length>0){
				data[i].children = temp;
			}
		}
	}
		return result;
}

exports.list = function(req,res,next){
	DataModel.find({},function(err,data){
		var rpTree=reverseTree(data,null);
		res.json(rpTree);
	})
}



exports.getNews=function(req,res,next){
	var options=[];
	if(req.body._id){
		options=req.body;
	}
	DataModel.findOne(options,function(err,data){
		var ids=[data._id];
		data.getChildren().then(function(child){
			for(var i=0;i<child.length;i++){
				ids.push(child[i]._id);

			}
			res.json(res);
		})
	})
}